define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'aps/workorder/index',
                    add_url: 'aps/workorder/add',
                    edit_url: '',
                    del_url: 'aps/workorder/del',
                    multi_url: 'aps/workorder/multi',
                    table: 'aps_workorder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
//                showColumns: false,
                showExport: false,
                search: false,
                dblClickToEdit: false, //是否啟用雙擊編輯
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'order.name', title: __('Order.name')},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'MainProcessName', title: __('規格'), operate:false},
                        {field: 'ProductNumber', title: __('產品品號'), operate:false},
                        {field: 'ProductName', title: __('產品品名'), operate:false},
                        //{field: 'number', title: __('Number')},
                        {field: 'num', title: __('Num')},
                        {field: 'planlog.switch', title: __('排程開關'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'planlog.state', title: __('工單狀態'), defaultValue:__('未完成'), searchList: {"未完成":__('未完成'),"已完成":__('已完成')}, formatter: Table.api.formatter.normal, sortable: true},
                        {field: 'startdate', title: __('Startdate'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'finishdate', title: __('Finishdate'), operate:'RANGE', addclass:'datetimerange'},

                        //{field: 'mainprocess.name', title: __('Mainprocess.name')},

                        //{field: 'subprocess.name', title: __('Subprocess.name')},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'order.id', title: __('Order.id')},
                        //{field: 'order.number', title: __('Order.number')},
                        //{field: 'order.startdate', title: __('Order.startdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'order.finishdate', title: __('Order.finishdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'order.create_time', title: __('Order.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'order.update_time', title: __('Order.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'mainprocess.id', title: __('Mainprocess.id')},
                        //{field: 'mainprocess.number', title: __('Mainprocess.number')},
                        //{field: 'mainprocess.manhours', title: __('Mainprocess.manhours'), operate:'BETWEEN'},
                        //{field: 'mainprocess.state', title: __('Mainprocess.state')},
                        //{field: 'mainprocess.create_time', title: __('Mainprocess.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'mainprocess.update_time', title: __('Mainprocess.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'subprocess.id', title: __('Subprocess.id')},
                        //{field: 'subprocess.mainprocess_id', title: __('Subprocess.mainprocess_id')},
                        //{field: 'subprocess.number', title: __('Subprocess.number')},
                        //{field: 'subprocess.state', title: __('Subprocess.state')},
                        //{field: 'subprocess.create_time', title: __('Subprocess.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'subprocess.update_time', title: __('Subprocess.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                var myColumns=[];
                $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                    myColumns.push(item.field);
                });
                $.cookie('fa_aps_workorder', JSON.stringify(myColumns), { expires: 365, path: '/'});
            });
            if($.cookie('fa_aps_workorder')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_aps_workorder')), function(i, item) {
                    table.bootstrapTable('hideColumn', item);
                });
            }

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});